# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
  # Pick a unique cookie name to distinguish our session data from others'
  session :session_key => '_ereader_session_id'

  before_filter :login_required, :except => [:login, :signup]

  include ApplicationHelper

  def login_required
    if user?
      return true
    end
    store_location
    access_denied
  end

  def access_denied
    flash[:notice] = 'Please login first'
    redirect_to :controller => "users", :action => "login"
  end

  def store_location
    session['return-to'] = request.request_uri
  end

  def user?
    not session[:user].nil?
  end

  def generate_blank
    case request.method
    when :get
      @user = User.new
      render
      return true
    end
    return false
  end


    # move to the last store_location call or to the passed default one
  def redirect_to_stored_or_default(default=nil)
    if session['return-to'].nil?
      redirect_to default
    else
      redirect_to_url session['return-to']
      session['return-to'] = nil
    end
  end


end
